package github.kawaiior.botapiserver.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

public class BotApiInterceptor implements HandlerInterceptor {

    private final Logger logger = LoggerFactory.getLogger(BotApiInterceptor.class);

    private static final String BOT_TOKEN = "fkU74yQo6E1uRfdnFPmscFFd9YBUGHzra0kQrrCVV7dD0Pidzh";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        String token = request.getParameter("token");
        if (!BOT_TOKEN.equals(token)){
            String requestMethod = request.getMethod();
            String requestURI = request.getRequestURI();
            String requestIP = request.getRemoteAddr();

            logger.info("{} - {} - {} - valid token !", requestIP, requestMethod, requestURI);
            return false;
        }
        return true;
    }
}
